package com.cpj.bsyyzx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpj.bsyyzx.entity.*;
import com.cpj.bsyyzx.mapper.FileNumMapper;
import com.cpj.bsyyzx.service.IBedRecordService;
import com.cpj.bsyyzx.service.IFileNumService;
import com.cpj.bsyyzx.service.IGoToRegisterService;
import com.cpj.bsyyzx.service.IUserService;
import com.cpj.bsyyzx.util.CommonResult;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 五个臭皮匠
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/goToRegister")
@Log4j
public class GoToRegisterController {
    @Resource
    IGoToRegisterService goToRegisterService;
    @Resource
    FileNumMapper fileNumMapper;
    @Resource
    IFileNumService fileNumService;
    @Autowired
    IBedRecordService bedRecordService;
    @Autowired
    BedRecordController bedRecordController;
    @Autowired
    IUserService userService;
    @Autowired
    UserController userController;
    @RequestMapping("/pagelist")
    CommonResult pagelist(Page page){
        QueryWrapper<GoToRegister> queryWrapper = new QueryWrapper<>();
        log.debug("分页查询");
        IPage<GoToRegister> rpage=goToRegisterService.selectPageVo(page,queryWrapper);
        return CommonResult.success(rpage);
    }
    @RequestMapping("/list")
    CommonResult list(Page page, GoToRegister goToRegister){
        QueryWrapper<GoToRegister> queryWrapper = new QueryWrapper<>();
        log.debug("分页查询");
        Page<GoToRegister> rpage=goToRegisterService.pageList(page,goToRegister);
        return CommonResult.success(rpage);
    }
    @RequestMapping("/add")
    @ResponseBody
    CommonResult add(GoToRegister goToRegister) {
        FileNum fileNum = fileNumMapper.selectFileByUser();
        Integer list = fileNum.getMaxnum();
        list += 1;
        fileNum.setMaxnum(list);
        fileNumService.saveOrUpdate(fileNum);
        goToRegister.setFileNum("goRegister" + list);

        return CommonResult.success(goToRegisterService.save(goToRegister));

    }
    @RequestMapping("save")
    @ResponseBody
    CommonResult save(GoToRegister goToRegister) {
        log.debug("保存");
        //获取用户床位信息
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("record_num",goToRegister.getFileNum());
        BedRecord bedRecord=bedRecordService.getOne(wrapper);
        bedRecord.setState("离开");
        bedRecordController.updateDoing(bedRecord);
        //获取用户表信息
        wrapper=new QueryWrapper();
        wrapper.eq("file_num",goToRegister.getFileNum());
        User user=userService.getOne(wrapper);
        userController.updateToLeft(user.getUserId());

        boolean success = goToRegisterService.saveOrUpdate(goToRegister);
        return  CommonResult.success(success);
    }
    @RequestMapping("toedit")
    @ResponseBody
    CommonResult toedit(ModelMap modelMap, Integer id) {
        log.debug("跳转到修改页面");
//        User user = userService.getById(id);
//        modelMap.addAttribute("user", user);
        return CommonResult.success(goToRegisterService.getById(id));
    }
    @RequestMapping("del/{id}")
    @ResponseBody
    CommonResult delete(@PathVariable Integer id) {
        log.debug("根据主键删除:" + id);
        boolean success = goToRegisterService.removeById(id);
        return CommonResult.success(success);
    }

    @RequestMapping("returnBack")
    @ResponseBody
    CommonResult returnBack(Integer goId){
        GoToRegister goToRegister=goToRegisterService.getById(goId);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("record_num",goToRegister.getFileNum());
        BedRecord bedRecord=bedRecordService.getOne(wrapper);
        bedRecord.setState("正常");
        //将床位的离开改为正常
        bedRecordController.updateDoing(bedRecord);
        goToRegister.setFactTime(LocalDateTime.now());
        goToRegister.setState("已返回");
        //将用户的信息修改回来
        wrapper=new QueryWrapper();
        wrapper.eq("file_num",goToRegister.getFileNum());
        User user=userService.getOne(wrapper);
        userController.comeBack(user.getUserId());

        boolean success = goToRegisterService.saveOrUpdate(goToRegister);
        return  CommonResult.success(success);
    }
}
